package com.atguigu.tingshu.order.strategy;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderDerate;
import com.atguigu.tingshu.model.order.OrderDetail;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.mapper.OrderDerateMapper;
import com.atguigu.tingshu.order.mapper.OrderDetailMapper;
import com.atguigu.tingshu.order.mapper.OrderInfoMapper;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;

public interface SaveOrderStrategy {

    // 定义保存订单的接口方法
    public abstract OrderInfoVo saveOrder(OrderInfoVo orderInfoVo) ;

    // 定义获取支付方式的方法
    public abstract String getPayWay() ;

    // 保存订单基本数据以及明细数据和减免明细数据
    public default void saveOrderInfoAndDetailAndDerateInfo(OrderInfoVo orderInfoVo , OrderInfoMapper orderInfoMapper ,
                                                            OrderDetailMapper orderDetailMapper ,
                                                            OrderDerateMapper orderDerateMapper) {

        // 获取当前登录用户的id
        Long userId = AuthContextHolder.getUserId();

        // 保存订单相关的数据：1、保存订单的基本数据   2、保存订单的明细数据   3、保存订单减免明细数据
        OrderInfo orderInfo = new OrderInfo();             // 创建OrderInfo对象，封装需要保存到订单的数据
        orderInfo.setUserId(userId);

        // 从明细列表中获取第一条明细数据，从该明细数据中获取明细名称，将其作为订单标题
        List<OrderDetailVo> orderDetailVoList = orderInfoVo.getOrderDetailVoList();
        OrderDetailVo orderDetailVo = orderDetailVoList.get(0);
        orderInfo.setOrderTitle(orderDetailVo.getItemName());
        orderInfo.setOrderNo(orderInfoVo.getTradeNo());
        orderInfo.setOrderStatus(SystemConstant.ORDER_STATUS_UNPAID);
        orderInfo.setOriginalAmount(orderInfoVo.getOriginalAmount());
        orderInfo.setDerateAmount(orderInfoVo.getDerateAmount());
        orderInfo.setOrderAmount(orderInfoVo.getOrderAmount());
        orderInfo.setItemType(orderInfoVo.getItemType());
        orderInfo.setPayWay(orderInfoVo.getPayWay());

        // 保存订单数据
        orderInfoMapper.insert(orderInfo);

        // 保存订单明细数据
        orderDetailVoList.forEach(odv -> {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderInfo.getId());             // 订单的id
            BeanUtils.copyProperties(odv, orderDetail);
            orderDetailMapper.insert(orderDetail);
        });

        // 保存订单减免明细数据
        List<OrderDerateVo> orderDerateVoList = orderInfoVo.getOrderDerateVoList();
        if (!CollectionUtils.isEmpty(orderDerateVoList)) {
            orderDerateVoList.forEach(orderDerateVo -> {
                OrderDerate orderDerate = new OrderDerate();
                orderDerate.setOrderId(orderInfo.getId());
                BeanUtils.copyProperties(orderDerateVo, orderDerate);
                orderDerateMapper.insert(orderDerate);
            });
        }

        // 执行后续操作
        this.saveOrderAfterProcess(orderInfoVo);

    }

    // 保存完订单数据以后的后续操作抽象方法
    public abstract void saveOrderAfterProcess(OrderInfoVo orderInfoVo) ;

}